package com.dh.project.controller;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dh.project.model.*;
import com.dh.project.model.push.PageType;
import com.dh.project.other.ModelRes;
import com.dh.project.service.*;
import com.dh.project.utils.*;
import com.dh.project.view.CouponView;
import com.dh.project.view.CustomerIdeaListView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.resources.Messages_pt_BR;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/** 客户操作Controller 11111
 * Created by admin on 2017/3/29.
 */
@Controller
public class CustomerController {

    private CustomerInfoService customerInfoService;

    private CustomerAddressService customerAddressService;

    private CustomerCouponService customerCouponService;

    private CouponService couponService;

    private CouponPeopleService couponPeopleService;

    private AdminService adminService;

    private CustomerIdeaService customerIdeaService;

    private CustomerInsuranceService customerInsuranceService;

    private IndoorpriceService indoorpriceService;

    @Autowired
    public CustomerController(CustomerInfoService customerInfoService, CustomerAddressService customerAddressService, CustomerCouponService customerCouponService,
                              CouponService couponService, CouponPeopleService couponPeopleService, AdminService adminService, CustomerIdeaService customerIdeaService,
                              CustomerInsuranceService customerInsuranceService,IndoorpriceService indoorpriceService) {
        this.customerInfoService = customerInfoService;
        this.customerAddressService = customerAddressService;
        this.customerCouponService = customerCouponService;
        this.couponService = couponService;
        this.couponPeopleService = couponPeopleService;
        this.adminService = adminService;
        this.customerIdeaService = customerIdeaService;
        this.customerInsuranceService = customerInsuranceService;
        this.indoorpriceService = indoorpriceService;
    }


    @RequestMapping(value = "testController", produces = "text/html;charset=utf-8")
    public String testController(Model model) {
        Map map = new HashMap();
        List<CustomerInfo> customerInfos = customerInfoService.selectByMap(map);
        model.addAttribute("customerInfos", customerInfos);
        return "customer_info";
    }

    /**
     * 客户信息列表
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "selectCustomerList", produces = "text/html;charset=utf-8")
    public String selectCustomerList(CustomerInfo customerInfo, int page, Model model) {
        Map map = new HashMap();
        map.put("customer_id", customerInfo.getCustomer_id());
//        map.put("customer_name",customerInfo.getCustomer_name());
        map.put("customer_phone", customerInfo.getCustomer_phone());

        map.put("page_start", (page - 1) * 10);
        map.put("page_end", 10);

        List<CustomerInfo> customerInfos = customerInfoService.selectCustomerListByMap(map);

        PageModel pageModel = customerInfoService.selectCustomerListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel, page, 10);
        pageInfo.setPage_href(PageUtil.getPageHref(customerInfo, 2));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(), 10));

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("customerInfos", customerInfos);
        model.addAttribute("customerInfo", customerInfo);
        return "customer_info";
    }

    /**
     * 修改客户信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateCustomerInfo", produces = "text/html;charset=utf-8")
    public Object updateCustomerInfo(CustomerInfo customerInfo1) {
        CustomerInfo customerInfo = customerInfoService.selectById(Long.valueOf(customerInfo1.getCustomer_id()));
        if (!StringUtil.isBlank(customerInfo1.getCustomer_name())) {
            customerInfo.setCustomer_name(customerInfo1.getCustomer_name());
        }

        if (!StringUtil.isBlank(customerInfo1.getCustomer_phone())) {
            Map map = new HashMap();
            map.put("customer_phone", customerInfo1.getCustomer_phone());
            List<CustomerInfo> customerInfos = customerInfoService.selectByMap(map);
            if (customerInfos.size() > 1) {
                if (customerInfos.get(0).getCustomer_id() != customerInfo1.getCustomer_id()) {
                    return new ModelRes(ModelRes.Status.ERROR, "该手机号码已存在，修改失败!");
                }
            } else {
                customerInfo.setCustomer_phone(customerInfo1.getCustomer_phone());
            }
        }
        if (customerInfo1.getIs_vip() != null) {
            customerInfo.setIs_vip(customerInfo1.getIs_vip());
        }
        if (customerInfo1.getIs_freeze() != null) {
            customerInfo.setIs_freeze(customerInfo1.getIs_freeze());
        }
        boolean isSuccess = customerInfoService.updateById(customerInfo);
        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "修改失败!");
        }
    }

    /**
     * 查询客户信息详情
     *
     * @param customer_id
     * @return
     */
    @RequestMapping(value = "selectCustomerDetail", produces = "text/html;charset=utf-8")
    public String selectCustomerDetail(int customer_id, Model model) {
        CustomerInfo customerInfo = customerInfoService.selectById(Long.valueOf(customer_id));
        List li = new ArrayList();
        Map map = new HashMap();
        map.put("customer_id", customer_id);

        Map customerMap = new HashMap();
        customerMap.put("customer_id", customer_id);
        customerMap.put("isDel", 1);
        List<CustomerAddress> customerAddresses = customerAddressService.selectByMap(customerMap);
        for (CustomerAddress address: customerAddresses) {
            List<Insurance> insuranceByAddress_id = customerInsuranceService.getInsuranceByAdd(address.getAddress_id());

            if (insuranceByAddress_id==null||insuranceByAddress_id.size()==0){
                address.setIsInsurance("普通地址");
                customerInfo.setIs_vip(1);
            }else{
                address.setIsInsurance("维保地址");
                customerInfo.setIs_vip(2);
            }
        }
        customerInfoService.updateById(customerInfo);
        CustomerInfo customerInfo1 = customerInfoService.selectById(customerInfo.getCustomer_id());

        List<CouponView> couponViews = customerCouponService.selectCouponByMap(map);
        /*for (CustomerAddress address: customerAddresses
             ) {
            List<Insurance> baseInsurance = customerInsuranceService.getInsuranceByAddress_id(address.getAddress_id());
                    li.add(baseInsurance);

        }*/
       // model.addAttribute("li",li);


        model.addAttribute("couponViews", couponViews);
        model.addAttribute("customerInfo", customerInfo1);
        model.addAttribute("customerAddresses", customerAddresses);
        return "customer_detail";
    }

    /**
     * 添加房产
     *
     * @param customerAddress
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertAddress", produces = "text/html;charset=utf-8")
    public Object insertAddress(CustomerAddress customerAddress) {
        customerAddress.setIsDel(1);
        customerAddress.setCreatetime(String.valueOf(new Date().getTime()));
        customerAddress.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = customerAddressService.insert(customerAddress);
        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "添加失败!");
        }
    }

    /**
     * 修改房产
     *
     * @param customerAddress
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateAddress", produces = "text/html;charset=utf-8")
    public Object updateAddress(CustomerAddress customerAddress) {
        CustomerAddress customerAddress1 = customerAddressService.selectById(customerAddress.getAddress_id());

        if (customerAddress.getAddress() != null) {
            customerAddress1.setAddress(customerAddress.getAddress());
        }

        if (customerAddress.getHouse_number() != null) {
            customerAddress1.setHouse_number(customerAddress.getHouse_number());
        }
        customerAddress.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = customerAddressService.updateById(customerAddress1);
        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "绑定失败!");
        }
    }


    /**
     * 删除客户房产信息
     *
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteAddress", produces = "text/html;charset=utf-8")
    public Object deleteAddress(int admin_id) {
        CustomerAddress customerAddress = customerAddressService.selectById(admin_id);
        customerAddress.setIsDel(2);
        boolean isSuccess = customerAddressService.updateById(customerAddress);
        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "删除失败!");
        }
    }

    //查询上门服务费接口

    @RequestMapping(value = "getIndoorPrice",produces = "text/html;charset=utf-8")
    public Object getIndoorPrice(Model model){
       // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Indoor_price indoorPrice = indoorpriceService.selectById(1);
        model.addAttribute("indoor_price",indoorPrice.getIndoor_price());
        model.addAttribute("updateTime",indoorPrice.getUpdateTime());
        return "indoor_price";
        /*if(indoorPrice==null){
            return new ModelRes(ModelRes.Status.ERROR,"服务费未设定！");

        }else{
            Map map =  new HashMap();
            map.put("indoor_price",indoorPrice.getIndoor_price());
            return new ModelRes(map);
        }*/
    }
//修改上门服务费
    @RequestMapping(value = "updateIndoorPrice",produces = "text/html;charset=utf-8")
    public Object updateIndoorPrice(Indoor_price indoor_price,Model model){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm");
        indoor_price.setId(1);
        String date = simpleDateFormat.format(new Date());
        String time = simpleDateFormat2.format(new Date());
        indoor_price.setUpdateTime(date+"T"+time);
        indoorpriceService.updateById(indoor_price);
        Indoor_price indoorPrice = indoorpriceService.selectById(1);
        model.addAttribute("indoor_price",indoorPrice.getIndoor_price());
        System.out.println(indoor_price.getUpdateTime());
        model.addAttribute("updateTime",indoor_price.getUpdateTime());
        return "indoor_price";

    }


    //绑定维保

    @RequestMapping(value = "insertOrUpdateInsurance", produces = "text/html;charset=utf-8")
    public Object insertOrUpdateInsurance(Insurance insurance,Model model) {
        insurance.setCurrDate(String.valueOf(new Date().getTime()));
        if(insurance.getStatus()==0){
            insurance.setLastDate("0");
        }else{
            try {

                CustomerAddress customerAddress = customerAddressService.selectById(insurance.getAddress_id());
                CustomerInfo customerInfo = customerInfoService.selectById(customerAddress.getCustomer_id());
                //Insurance insurance1 = customerInsuranceService.selectById(insurance.getId());
                Map map = new HashMap();
                map.put("insType",insurance.getType()+insurance.getName());
                map.put("address",customerAddress.getAddress()+customerAddress.getHouse_number());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
                String now = sdf.format(new Date());
                String[] split = insurance.getLastDate().split("_");
                String last = "";
                for (int i = 0; i <split.length ; i++) {
                    if(i==0){
                       last =  split[i]+"年";
                    }else if(i==1){
                        last = last+split[i]+"月";
                    }else if(i==2){
                        last = last+split[i]+"日";
                    }
                }
                map.put("createTime",now);
                map.put("lastTime",last);
               DayuSMSUtil.sendSms(map.toString(),DayuSMSUtil.APPINS,customerInfo.getCustomer_phone());
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
       boolean b = customerInsuranceService.updateById(insurance);

        List<Insurance> baseInsurance = customerInsuranceService.getInsuranceByAddress_id(insurance.getAddress_id());
        model.addAttribute("baseInsurance",baseInsurance);

        return "insurance";
       /* if(b){
             return "";
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "绑定失败!");
        }*/
        /*int i = customerInsuranceService.updateInsuranceByName(insurance);
        if (i == 1) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "绑定失败!");

        }*/
    }

    //根据地址查询维保
    @RequestMapping(value = "getInsuranceByAddress_id", produces = "text/html;charset=utf-8")
    public String getInsuranceByAddress_id(Integer address_id,Model model) {

        List<Insurance> baseInsurance = customerInsuranceService.getInsuranceByAddress_id(address_id);
        for (Insurance ins:baseInsurance) {
            if(ins.getLastDate()==null||ins.getLastDate().equals("")||ins.getLastDate().equals("0")){

            }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                String t = ins.getLastDate();
                Date date = null;
                try {
                    date = sdf.parse(t);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                long firstTime = date.getTime()-7776000000l;
                long secondTime = date.getTime()-15552000000l;
                ins.setFirstTime(firstTime);
                ins.setSecondTime(secondTime);
            }
        }


        if (baseInsurance == null || baseInsurance.size() < 1) {
          /*  System.out.println("未查询信息！");
            return new ModelRes(ModelRes.Status.ERROR, "该用户没绑定维保!");*/
            System.out.println("-----------------------------------");
            return "insurance";
        } else {
           /* System.out.println(baseInsurance.toString());*/
            model.addAttribute("baseInsurance",baseInsurance);

            return "insurance";
        }
    }

    //根据客户id查询维保
    @ResponseBody
    @RequestMapping(value = "getInsuranceByCustomer_id", produces = "text/html;charset=utf-8")
    public Object getInsuranceByCustom_id(int customer_id) {
        List<Integer> address = customerAddressService.getAddress_id(customer_id);

        if(address==null||address.size()<1){
            return new ModelRes(ModelRes.Status.ERROR,"该客户未添加地址！");
        }

        List list1 = new ArrayList();
        List list2 = new ArrayList();
        List list3 = new ArrayList();
        List list4 = new ArrayList();
        List li = new ArrayList();
        Map map1 = new HashMap();
        Map map2 = new HashMap<String, Object>();
        Map map3 = new HashMap();
        Map map4 = new HashMap();
        ArrayEntity array = new ArrayEntity();

        if (address != null && address.size() >= 1) {
            BaseVO vo1 = new BaseVO();
            BaseVO vo2 = new BaseVO();
            BaseVO vo3 = new BaseVO();
            BaseVO vo4 = new BaseVO();
            BaseVO vo5 = new BaseVO();
            BaseVO vo6 = new BaseVO();
            BaseVO vo7 = new BaseVO();
            BaseVO vo8 = new BaseVO();
            BaseVO vo9 = new BaseVO();
            for (int j = 0; j < address.size(); j++) {
                int address_id = address.get(j);

                List<Insurance> baseInsurance = customerInsuranceService.getInsuranceByAddress_id(address_id);

                if(baseInsurance==null||baseInsurance.size()<1){
                    return new ModelRes(ModelRes.Status.ERROR,"地址未绑定保险！");
                }
                for (Insurance ins : baseInsurance) {

                    if (ins.getType().equals("中央空调")) {
                        if (ins.getName().equals("水系统") && ins.getStatus() == 1) {
                            vo2.setName(ins.getName());
                            vo2.setStatus(ins.getStatus());
                            if(!list1.contains(vo2)){
                                list1.add(vo2);
                            }else if(list1.contains(vo2)&&vo2.getStatus()==0){
                                list1.remove(vo2);
                                list1.add(vo2);
                            }
                        } else if (ins.getName().equals("水系统") && ins.getStatus() == 0) {
                            vo2.setName(ins.getName());
                            vo2.setStatus(ins.getStatus());
                            if(!list1.contains(vo2)) {
                                list1.add(vo2);
                            }
                        }
                        if (ins.getName().equals("vrv多联机") && ins.getStatus() == 1) {
                            vo1.setName(ins.getName());
                            vo1.setStatus(ins.getStatus());
                            if(!list1.contains(vo1)){
                                list1.add(vo1);
                            }else if(list1.contains(vo1)&&vo1.getStatus()==0){
                                list1.remove(vo1);
                                list1.add(vo1);
                            }
                        } else if (ins.getName().equals("vrv多联机") && ins.getStatus() == 0) {
                            vo1.setName(ins.getName());
                            vo1.setStatus(ins.getStatus());
                            if(!list1.contains(vo1)) {
                                list1.add(vo1);
                            }
                        }
                        map1.put("type", ins.getType());
                        map1.put("array", list1);
                    } else if (ins.getType().equals("地暖")) {
                        if (ins.getName().equals("水采暖") && ins.getStatus() == 1) {
                            vo3.setName(ins.getName());
                            vo3.setStatus(ins.getStatus());
                            if(!list2.contains(vo3)){
                                list2.add(vo3);
                            }else if(list2.contains(vo3)&&vo3.getStatus()==0){
                                list2.remove(vo3);
                                list2.add(vo3);
                            }
                        } else if (ins.getName().equals("水采暖") && ins.getStatus() == 0) {
                            vo3.setName(ins.getName());
                            vo3.setStatus(ins.getStatus());
                            if(!list2.contains(vo3)) {
                                list2.add(vo3);
                            }
                        }
                        if (ins.getName().equals("电采暖") && ins.getStatus() == 1) {
                            vo4.setName(ins.getName());
                            vo4.setStatus(ins.getStatus());
                            if(!list2.contains(vo4)){
                                list2.add(vo4);
                            }else if(list2.contains(vo4)&&vo4.getStatus()==0){
                                list2.remove(vo4);
                                list2.add(vo4);
                            }
                        } else if (ins.getName().equals("电采暖") && ins.getStatus() == 0) {
                            vo4.setName(ins.getName());
                            vo4.setStatus(ins.getStatus());
                            if(!list2.contains(vo4)) {
                                list2.add(vo4);
                            }
                        }
                        map2.put("type", ins.getType());
                        map2.put("array", list2);
                    } else if (ins.getType().equals("新风")) {
                        if (ins.getName().equals("净化新风") && ins.getStatus() == 1) {
                            vo5.setName(ins.getName());
                            vo5.setStatus(ins.getStatus());
                            if(!list3.contains(vo5)){
                                list3.add(vo5);
                            }else if(list3.contains(vo5)&&vo5.getStatus()==0){
                                list3.remove(vo5);
                                list3.add(vo5);
                            }
                        } else if (ins.getName().equals("净化新风") && ins.getStatus() == 0) {
                            vo5.setName(ins.getName());
                            vo5.setStatus(ins.getStatus());
                            if(!list3.contains(vo5)) {
                                list3.add(vo5);
                            }
                        }
                        if (ins.getName().equals("无净化新风") && ins.getStatus() == 1) {
                            vo6.setName(ins.getName());
                            vo6.setStatus(ins.getStatus());
                            if(!list3.contains(vo6)){
                                list3.add(vo6);
                            }else if(list3.contains(vo6)&&vo6.getStatus()==0){
                                list3.remove(vo6);
                                list3.add(vo6);
                            }
                        } else if (ins.getName().equals("无净化新风") && ins.getStatus() == 0) {
                            vo6.setName(ins.getName());
                            vo6.setStatus(ins.getStatus());
                            if(!list3.contains(vo6)) {
                                list3.add(vo6);
                            }
                        }
                        map3.put("type", ins.getType());
                        map3.put("array", list3);
                    } else if (ins.getType().equals("水处理")) {
                        if (ins.getName().equals("净水") && ins.getStatus() == 1) {
                            vo7.setName(ins.getName());
                            vo7.setStatus(ins.getStatus());
                            if(!list4.contains(vo7)){
                                list4.add(vo7);
                            }else if(list4.contains(vo7)&&vo7.getStatus()==0){
                                list4.remove(vo7);
                                list4.add(vo7);
                            }
                        } else if (ins.getName().equals("净水") && ins.getStatus() == 0) {
                            vo7.setName(ins.getName());
                            vo7.setStatus(ins.getStatus());
                            if(!list4.contains(vo7)) {
                                list4.add(vo7);
                            }
                        }
                        if (ins.getName().equals("软水") && ins.getStatus() == 1) {
                            vo8.setName(ins.getName());
                            vo8.setStatus(ins.getStatus());
                            if(!list4.contains(vo8)){
                                list4.add(vo8);
                            }else if(list4.contains(vo8)&&vo8.getStatus()==0){
                                list4.remove(vo8);
                                list4.add(vo8);
                            }
                        } else if (ins.getName().equals("软水") && ins.getStatus() == 0) {
                            vo8.setName(ins.getName());
                            vo8.setStatus(ins.getStatus());
                            if(!list4.contains(vo8)) {
                                list4.add(vo8);
                            }
                        }
                        if (ins.getName().equals("直饮水") && ins.getStatus() == 1) {
                            vo9.setName(ins.getName());
                            vo9.setStatus(ins.getStatus());
                            if(!list4.contains(vo9)){
                                list4.add(vo9);
                            }else if(list4.contains(vo9)&&vo9.getStatus()==0){
                                list4.remove(vo9);
                                list4.add(vo9);
                            }
                        } else if (ins.getName().equals("直饮水") && ins.getStatus() == 0) {
                            vo9.setName(ins.getName());
                            vo9.setStatus(ins.getStatus());
                            if(!list4.contains(vo9)) {
                                list4.add(vo9);
                            }
                        }
                        map4.put("type", ins.getType());
                        map4.put("array", list4);
                    }


                }


            }
            if (list1.size() >= 1) {
                li.add(map1);
            }
            if (list2.size() >= 1) {
                li.add(map2);
            }
            if (list3.size() >= 1) {
                li.add(map3);
            }
            if (list4.size() >= 1) {
                li.add(map4);
            }



        }

        return new ModelRes(li);

    }



    /**
     * 添加优惠券
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addCoupon", produces = "text/html;charset=utf-8")
    public Object addCoupon(int customer_id, int admin_id) {
        Admin admin = adminService.selectById(admin_id);

        //查询是否有正在进行中的优惠券活动
        Map map = new HashMap();
        map.put("coupon_status", 1);
        List<Coupon> coupons = couponService.selectByMap(map);
        if (coupons.size() == 0) {
            return new ModelRes(ModelRes.Status.ERROR, "当前没有优惠券活动领取，添加失败!");
        }

        //客户信息
        CustomerInfo customerInfo = customerInfoService.selectById(customer_id);

        //判断是否用户第一次领取
        Map customerMap = new HashMap();
        customerMap.put("customer_phone", customerInfo.getCustomer_phone());
        customerMap.put("coupon_id", coupons.get(0).getId());
        customerMap.put("admin_id", 0);
        List<CouponePeople> couponePeopleList = couponPeopleService.selectByMap(customerMap);
        if (couponePeopleList.size() > 0) {
            return new ModelRes(ModelRes.Status.ERROR, "您已领取过该活动优惠券，不能再次领取!");
        }

        //判断是否超过领取数量
        if ((coupons.get(0).getOut_count() - 1) >= 0) {
            //添加优惠券领取记录
            CouponePeople couponePeople = new CouponePeople();
            couponePeople.setCreatetime(String.valueOf(new Date().getTime()));
            couponePeople.setCoupon_id(coupons.get(0).getId());
            couponePeople.setCustomer_phone(customerInfo.getCustomer_phone());
            if (null != admin) {
                couponePeople.setAdmin_login_name(admin.getAdmin_login_name());
                couponePeople.setAdmin_id(admin.getAdmin_id());
            } else {
                couponePeople.setAdmin_login_name("无");
                couponePeople.setAdmin_id(0);
            }
            couponPeopleService.insert(couponePeople);

            //修改优惠券领取总数
            coupons.get(0).setOut_count(coupons.get(0).getOut_count() - 1);
            couponService.updateById(coupons.get(0));

            //客户信息添加优惠券
            CustomerCoupon customerCoupon = new CustomerCoupon();
            customerCoupon.setIs_use(1);
            customerCoupon.setIs_out(1);
            customerCoupon.setCreatetime(String.valueOf(new Date().getTime()));
            customerCoupon.setCoupon_id(coupons.get(0).getId());
            customerCoupon.setCustomer_id(customer_id);
            customerCouponService.insert(customerCoupon);
            List<String> aliasList = new ArrayList<>();
            aliasList.add(customerInfo.getCustomer_id().toString());
            PushUtil.pushOne(aliasList, "您已收到一张新的优惠券，请点击查看详情!", JSON.toJSON(new PageType(2,customerInfo.getCustomer_id().toString())));
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "当前优惠券活动已领取完，添加优惠券失败!");
        }
    }

    /**
     * 领取优惠券
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCoupon", produces = "text/html;charset=utf-8")
    public Object getCoupon(String telephone) {
        if (telephone == null || telephone.length()!=11 || telephone.indexOf("1")!=0) {
            return new ModelRes(ModelRes.Status.ERROR, "您输入的手机号不合法!");
        }

        //查询是否有正在进行中的优惠券活动
        Map map = new HashMap();
        Map map2 = new HashMap();
        map2.put("customer_phone",telephone);
        map.put("coupon_status", 1);
        List<Coupon> coupons = couponService.selectByMap(map);
        if (coupons.size() == 0) {
            return new ModelRes(ModelRes.Status.ERROR, "当前没有优惠券活动领取，领取失败!");
        }

        //客户信息
        List<CustomerInfo> lists = customerInfoService.selectByMap(map2);
        //判断是否用户第一次领取
        Map customerMap = new HashMap();
        customerMap.put("customer_phone", telephone);
        customerMap.put("coupon_id", coupons.get(0).getId());
        List<CouponePeople> couponePeopleList = couponPeopleService.selectByMap(customerMap);
        if (couponePeopleList.size() > 0) {
            return new ModelRes(ModelRes.Status.ERROR, "您已领取过该活动优惠券，不能再次领取!");
        }
        if(lists.size()==0){

            //判断是否超过领取数量
            if ((coupons.get(0).getOut_count() - 1) >= 0) {
                //添加优惠券领取记录
                CouponePeople couponePeople = new CouponePeople();
                couponePeople.setCreatetime(String.valueOf(new Date().getTime()));
                couponePeople.setCoupon_id(coupons.get(0).getId());
                couponePeople.setCustomer_phone(telephone);

                    couponePeople.setAdmin_login_name("客户端领取");
                    couponePeople.setAdmin_id(0);

                couponPeopleService.insert(couponePeople);

                //修改优惠券领取总数
                coupons.get(0).setOut_count(coupons.get(0).getOut_count() - 1);
                couponService.updateById(coupons.get(0));

                //客户信息添加优惠券
                CustomerCoupon customerCoupon = new CustomerCoupon();
                customerCoupon.setIs_use(1);
                customerCoupon.setIs_out(1);
                customerCoupon.setCreatetime(String.valueOf(new Date().getTime()));
                customerCoupon.setCoupon_id(coupons.get(0).getId());
                //客户未注册情况，客户id为0
                customerCoupon.setCustomer_id(0);
                customerCouponService.insert(customerCoupon);
              return new ModelRes(ModelRes.Status.SUCCESS,"领取成功！注册成功后可在您的优惠券页面查询你的优惠券详情!");
            } else {
                return new ModelRes(ModelRes.Status.ERROR, "当前优惠券活动已领取完，添加优惠券失败!");
            }

        }else{

            //判断是否超过领取数量
            if ((coupons.get(0).getOut_count() - 1) >= 0) {
                //添加优惠券领取记录
                CouponePeople couponePeople = new CouponePeople();
                couponePeople.setCreatetime(String.valueOf(new Date().getTime()));
                couponePeople.setCoupon_id(coupons.get(0).getId());
                couponePeople.setCustomer_phone(telephone);

                couponePeople.setAdmin_login_name("客户端领取");
                couponePeople.setAdmin_id(0);

                couponPeopleService.insert(couponePeople);

                //修改优惠券领取总数
                coupons.get(0).setOut_count(coupons.get(0).getOut_count() - 1);
                couponService.updateById(coupons.get(0));

                //客户信息添加优惠券
                CustomerCoupon customerCoupon = new CustomerCoupon();
                customerCoupon.setIs_use(1);
                customerCoupon.setIs_out(1);
                customerCoupon.setCreatetime(String.valueOf(new Date().getTime()));
                customerCoupon.setCoupon_id(coupons.get(0).getId());
                //客户未注册情况，客户id为0
                customerCoupon.setCustomer_id(lists.get(0).getCustomer_id());
                customerCouponService.insert(customerCoupon);
                List<String> aliasList = new ArrayList<>();
                aliasList.add(lists.get(0).getCustomer_id().toString());
                PushUtil.pushOne(aliasList, "您已收到一张新的优惠券，请点击查看详情!", JSON.toJSON(new PageType(2,lists.get(0).getCustomer_id().toString())));
                return new ModelRes(ModelRes.Status.SUCCESS,"领取成功！请登录后在您的优惠券页面查询你的优惠券详情!");
            } else {
                return new ModelRes(ModelRes.Status.ERROR, "当前优惠券活动已领取完，添加优惠券失败!");
            }
        }
    }

    /**
     * 删除客户优惠券信息
     *
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteCustomerCoupon", produces = "text/html;charset=utf-8")
    public Object deleteCustomerCoupon(int admin_id) {
        boolean isSuccess = customerCouponService.deleteById(admin_id);
        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "删除失败!");
        }
    }

    /**
     * 冻结客户
     *
     * @param customer_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "dongJieCustomerInfo", produces = "text/html;charset=utf-8")
    public Object dongJieCustomerInfo(int customer_id) {
        CustomerInfo customerInfo = customerInfoService.selectById(customer_id);
        if (customerInfo.getIs_freeze() == 1) {
            customerInfo.setIs_freeze(2);
        } else if (customerInfo.getIs_freeze() == 2) {
            customerInfo.setIs_freeze(1);
        }
        customerInfo.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = customerInfoService.updateById(customerInfo);
        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "操作失败!");
        }
    }

    /**
     * 查询客户意见列表
     *
     * @param customerIdeaListView
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "selectCustomerIdeaList", produces = "text/html;charset=utf-8")
    public String selectCustomerIdeaList(CustomerIdeaListView customerIdeaListView, int page, Model model) {
        Map map = new HashMap();
        map.put("page_start", (page - 1) * 10);
        map.put("page_end", 10);
        if (!StringUtil.isBlank(customerIdeaListView.getStart_date())) {
            map.put("start_date", customerIdeaListView.getStart_date());
        } else {
            map.put("start_date", null);
        }
        if (!StringUtil.isBlank(customerIdeaListView.getEnd_date())) {
            map.put("end_date", customerIdeaListView.getEnd_date());
        } else {
            map.put("end_date", null);
        }
        List<CustomerIdeaListView> customerIdeaListViews = customerIdeaService.selectCustomerIdeaListByMap(map);
        model.addAttribute("customerIdeaListViews", customerIdeaListViews);


        PageModel pageModel = customerIdeaService.selectCustomerIdeaListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel, page, 10);
        pageInfo.setPage_href(PageUtil.getPageHrefFour(customerIdeaListView, 1));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(), 10));

        model.addAttribute("pageInfo", pageInfo);
        return "customer_idea_list";
    }

    /**
     * 删除客户意见
     *
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteCustomerIdea", produces = "text/html;charset=utf-8")
    public Object deleteCustomerIdea(int admin_id) {
        boolean isSuccess = customerIdeaService.deleteById(admin_id);
        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "操作失败!");
        }
    }


}
